clear;clc;
Data=importdata('BCHAIN-MKPRU.csv')
data_o=Data.data
OUT_b=[]
p=3;
q=3;
Mdl=arima(p,1,q);
Mdl2=arima(1,1,1)
for day=20:1826
% GOLD
day
data=data_o(1:day);
% Yd=diff(data);
% yd_h_adf=adftest(Yd);
% yd_h_kpss=kpsstest(Yd);

% figure;
% autocorr(Yd);
% figure;
% parcorr(Yd);
try
EstMdl = estimate(Mdl,data);
[res,~,logL] = infer(EstMdl,data);
catch
EstMdl = estimate(Mdl2,data);
[res,~,logL] = infer(EstMdl,data);
end

% stdr = res/sqrt(EstMdl.Variance);
% figure('Name','残差检验');
% subplot(2,3,1);
% plot(stdr);
% title('Standardized Residuals');
% subplot(2,3,2);
% histogram(stdr,10);
% title('Standardized Residuals');
% subplot(2,3,3);
% autocorr(stdr);
% subplot(2,3,4);
% parcorr(stdr);
% subplot(2,3,5);
% qqplot(stdr);
% diffRes0 = diff(res);
% SSE0 = res'*res;
% DW0 = (diffRes0'*diffRes0)/SSE0;

step = 100; 
[forData,YMSE] = forecast(EstMdl,step,'Y0',data);
% lower = forData - 1.96*sqrt(YMSE);
% upper = forData + 1.96*sqrt(YMSE);

% figure();
% plot(data_o,'Color',[.7,.7,.7]);
% hold on
% h1 = plot(length(data):length(data)+step,[data(end);lower],'r:','LineWidth',2);
% plot(length(data):length(data)+step,[data(end);upper],'r:','LineWidth',2);
% h2 = plot(length(data):length(data)+step,[data(end);forData],'k','LineWidth',2);
% legend([h1 h2],'95% possible','predict','Location','NorthWest');
% title('Predict');
% hold off
temp=[forData(50);forData(100);forData(75)]
add_matrix=[day,temp'];
OUT_b=[OUT_b;add_matrix];
end
csvwrite('predict_b.csv',OUT_b)